package dongshi.dadday.testdongsirmultidatasourcestarter.controller;

import dongshi.dadday.testdongsirmultidatasourcestarter.entity.T;
import dongshi.dadday.testdongsirmultidatasourcestarter.service.TService;
import dongshi.daddy.dongsrmutitidatasoucespringbootstarter.holder.MyCurrenetDatasourceHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/multidatasource")
public class MultiDatasourceController {

    @Autowired
    private TService tService;

    @RequestMapping(value = "/hello")
    public void hello(
            HttpServletResponse response,
            String routeKey,
            int id
    ) throws Exception {
        System.out.println(">>>>>routekey is: " + routeKey + "<<<<<<");
        System.out.println(">>>>>id is: " + id + "<<<<<<");
        // 设置客户端传递的routekey，从而实现数据源的动态切换
        MyCurrenetDatasourceHolder.switchCurrentDataSourceConfig(routeKey);
        T t = tService.queryById(id);
        String result = t.getA() + "-" + t.getB();
        response.getWriter().write("welcome use multi datasource, " + result);
    }
}
